package com.bangbang.controller;

import com.bangbang.model.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("mongo")
public class MongoController {
    @Autowired
    private MongoTemplate mongoTemplate;

    @GetMapping
    public String testMongo(Long id){
        Comment comment = new Comment();
        comment.setCourseid(id);
        Criteria criteria = Criteria.where("courseId").is(id);
        List<Comment> objects = mongoTemplate.find(new Query(criteria),Comment.class);
        objects.stream().forEach(System.err::println);
        return "ok";
    }

    @PostMapping
    public String saveMongo(Comment comment){
        mongoTemplate.save(comment);
        return "ok";
    }

}
